/**
 * Copyright(C) 2012 Luvina Software Company
 *
 * ListUserManaged.java, 04/01/2013, bacdv
 */
package net.luvina.manageuser.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * StringUtil
 *
 * @author bacdv
 *
 */
public class StringUtil {
    /**
     * function check text is halfsize number
     *
     * @param String
     *            textToCheck
     * @return boolean: true if is halfsize number
     */
    public static boolean isHalfsizeNumber(String textToCheck) {
        return Pattern.matches("[0-9]+", textToCheck);
    }

    /**
     * getNowDay
     *
     * @return Now Day
     */
    public static int getNowDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date date = new Date();
        int day = Integer.parseInt(sdf.format(date));
        return day;
    }

    /**
     * getNowMonth
     *
     * @return Now Month
     */
    public static int getNowMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        Date date = new Date();
        int month = Integer.parseInt(sdf.format(date));
        return month;
    }

    /**
     * check is date
     *
     * @param year
     *            year
     * @param month
     *            month
     * @param day
     *            day
     * @return true if is date/ false if not date
     */
    public static boolean isDate(int year, int month, int day) {
        month -= 1;
        Calendar cal = Calendar.getInstance();
        cal.setLenient(false);
        try {
            cal.set(year, month, day);
            @SuppressWarnings("unused")
            Date date = cal.getTime();
        } catch (IllegalArgumentException e) {
            return false;
        }
        return true;
    }

    /**
     * Check String input is String kana
     *
     * @param text
     * @return text is String kana. false: text not String kana
     */
    public static boolean isKatakana(String text) {
        char[] c = text.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if ((Character.UnicodeBlock.of(c[i]) != Character.UnicodeBlock.KATAKANA)
                    && (!CommonUtil.isDigit(c[i]))
                    && (!Character.isWhitespace(c[i]))) {
                return false;
            }
        }
        return true;
    }

    /**
     * function to check Character is English
     *
     * @param textToCheck
     * @return boolean: true if is character english
     */
    public static boolean isCharacter(String textToCheck) {
        boolean isCharacterEnglish = true;
        int count = 0;
        char[] c = textToCheck.toCharArray();

        for (int i = 0; i < c.length; i++) {
            try {
                count = (String.valueOf(c[i])).getBytes().length;
            } catch (Exception e) {
                count = 0;
            }
            if (!Character.isWhitespace(c[i]) && count >= 2) {
                isCharacterEnglish = false;
            }
        }
        return isCharacterEnglish;
    }

    public static String getGroupDefault() {
        StringBuilder sqlCommand = new StringBuilder("");
        sqlCommand = sqlCommand.append(" SELECT ");
        sqlCommand = sqlCommand.append(" group_id ");
        sqlCommand = sqlCommand.append(" FROM mst_group ");
        sqlCommand = sqlCommand.append(" ORDER BY RAND() ");
        sqlCommand = sqlCommand.append(" LIMIT 1 ");
        return sqlCommand.toString();
    }

    public static String sqlSelect() {
        StringBuilder sqlCommand = new StringBuilder("");
        sqlCommand = sqlCommand.append(" SELECT  ");
        sqlCommand = sqlCommand.append(" u.user_id as userId, ");
        sqlCommand = sqlCommand.append(" u.full_name as fullName, ");
        sqlCommand = sqlCommand.append(" u.birthday as birthday, ");
        sqlCommand = sqlCommand.append(" u.email as email, ");
        sqlCommand = sqlCommand.append(" u.tel as tel, ");

        sqlCommand = sqlCommand.append(" g.group_name as groupName, ");

        sqlCommand = sqlCommand.append(" j.name_level as nameLevel, ");
        sqlCommand = sqlCommand.append(" tduj.end_date as endDate, ");
        sqlCommand = sqlCommand.append(" tduj.total as total ");

        sqlCommand = sqlCommand.append(" FROM tbl_user AS u ");
        sqlCommand = sqlCommand.append(" INNER JOIN mst_group AS g ");
        sqlCommand = sqlCommand.append(" ON u.group_id = g.group_id ");

        sqlCommand = sqlCommand
                .append(" LEFT JOIN tbl_detail_user_japan AS tduj ");
        sqlCommand = sqlCommand.append(" ON u.user_id = tduj.user_id ");

        sqlCommand = sqlCommand.append(" LEFT JOIN mst_japan AS j ");
        sqlCommand = sqlCommand.append(" ON tduj.code_level = j.code_level ");

        return sqlCommand.toString();
    }
}
